<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;
use think\exception\PDOException;
use Exception;


/**
 * 产品信息管理
 *
 * @icon fa fa-circle-o
 */
class Product extends Backend
{

    /**
     * Product模型对象
     * @var \app\admin\model\Products
     */
    protected $model = null;
    protected $searchFields = 'product_name';
    protected $noNeedRight = ['cate'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Products;
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                    ->with(['set'])
                    ->where($where)
                    ->where('is_deleted','N')
                    ->order($sort, $order)
                    ->paginate($limit);
            foreach ($list as $row) {
                $row->getRelation('set')->visible(['product_set_name']);
            }
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }
    public function add2(){
        return parent::add();
    }
    public function edit2($ids=null){
        return parent::edit($ids);
    }
      /**
     * 查看
     */
    public function index2()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                    ->with(['set'])
                    ->where($where)
                    ->where('is_deleted','N')
                    ->where('supplier_id','-1')
                    ->order($sort, $order)
                    ->paginate($limit);
            foreach ($list as $row) {
                $row->getRelation('set')->visible(['product_set_name']);
            }
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }
    public function cate(){
       $product_category = Db::name('product_category')->column('name,name as title');
       $supplier = Db::name('supplier')->column('id,supplier_name');
       $product_material = Db::name('product_material')->column('name,name as title');
       $product_purity = Db::name('product_purity')->column('name,name as title');
       $product_technology = Db::name('product_technology')->column('name,name as title');
       $product_skn = Db::name('product_skn')->column('name,name as title');
       return compact('product_category','supplier','product_material','product_purity','product_technology','product_skn');
    }
    /**
     * 删除
     *
     * @param $ids
     * @return void
     * @throws DbException
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function del($ids = null)
    {
        if (false === $this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ?: $this->request->post("ids");
        if (empty($ids)) {
            $this->error(__('Parameter %s can not be empty', 'ids'));
        }
        $pk = $this->model->getPk();
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            $this->model->where($this->dataLimitField, 'in', $adminIds);
        }
        $list = $this->model->where($pk, 'in', $ids)->update(['is_deleted'=>'Y']);
       
            $this->success();
        $this->error(__('No rows were deleted'));
    }
       // 收藏
       public function love(){
        $user_id = $this->auth->id;
        $id = input('id');
        $product_ids = Db::name('admin')->where('id',$user_id)->value('love_ids');
        $product_ids = explode(',',$product_ids);
        if(in_array($id,$product_ids)){
            foreach($product_ids as $k=>&$v){
                if($v==$id){
                    unset($product_ids[$k]);
                }
            }
        }else{
            $product_ids[]= $id;
        }
        $product_ids = implode(',',$product_ids);
        $product_ids = Db::name('user')->where('id',$user_id)->update(['love_ids'=>$product_ids]);
        return json_encode(['code'=>1]);
    }
    public function love_list(){
        $user_id = $this->auth->id;
        $city_id = input('city_id');
        $shop_ids = Db::name('user')->where('id',$user_id)->value('love_shop_ids');
       
        $ids = explode(',',trim($shop_ids));
        $is_str = ','.implode($ids);
        if($ids){
            $list = $this->model->whereIn('id',$ids)->orderRaw('field (id '.$is_str.')')->paginate();
            foreach ($list as $info) {
                $info->image_urls = cdnurl($info->image_urls,true);
            }
        }
        $result = array("code"=>1,"total" => $list->total(), "rows" => $list->items());
        return json_encode($result);
    }
    
    //展示
    public function list(){
        $op['shop_id'] =['>=',0];
        $product_category = input('product_category');
        if($product_category){
            $op['product_category'] = $product_category;
        }
        $product_material = input('product_material');
        if($product_material){
            $op['product_material'] = $product_material;
        }
        $product_purity = input('product_purity');
        if($product_purity){
            $op['product_purity'] = $product_purity;
        }
        $product_technology = input('product_technology');
        if($product_technology){
            $op['product_technology'] = $product_technology;
        }
        $product_skn = input('product_skn');
        if($product_skn){
            $op['product_skn'] = $product_skn;
        }
        $product_set = input('product_set');
        if($product_set){
            $op['product_set'] = $product_set;
            unset($op['shop_id']);
        }
        $list = $this->model->where($op)->paginate();
        foreach ($list as $info) {
            $info->image_urls = cdnurl($info->image_urls,true);
        }
        $result = array("code"=>1,"total" => $list->total(), "rows" => $list->items());
        return json_encode($result);
    }
 
}
